home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / OPENSAVE.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  327 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.11  $
  6. //
  7. // Implementation of OpenSave abstract, FileOpen, FileSave Common Dialog
  8. // classes
  9. //----------------------------------------------------------------------------
  10. #pragma hdrignore SECTION
  11. #include <owl/pch.h>
  12. #if !defined(OWL_OPENSAVE_H)
  13. # include <owl/opensave.h>
  14. #endif
  15.  
  16. OWL_DIAGINFO;
  17.  
  18. #if !defined(SECTION) || SECTION == 1
  19.  
  20. //
  21. //
  22. //
  23. TOpenSaveDialog::TData::TData(uint32 flags,
  24.                               char*  filter,
  25.                               char*  customFilter,
  26.                               char*  initialDir,
  27.                               char*  defExt,
  28.                               int    maxPath,
  29.                               int    filterIndex)
  30. :
  31.   Flags(flags), Error(0), FileName(0), Filter(0),
  32.   CustomFilter(customFilter), FilterIndex(filterIndex),
  33.   InitialDir(initialDir), DefExt(defExt),
  34.   MaxPath(maxPath ? maxPath : _MAX_PATH)
  35. {
  36.   FileName = new char[MaxPath];
  37.   *FileName = 0;
  38.   SetFilter(filter);
  39. #if defined(BI_PLAT_WIN32)
  40.   Flags |= OFN_LONGNAMES;
  41.  
  42.   //
  43.   // We currently cannot mix OFN_ENABLEHOOK & OFN_EXPLORER
  44.   // when running under NT 3.51[no new shell]
  45.   //
  46.   if (TSystem::Has3dUI())
  47.     Flags |= OFN_EXPLORER;
  48. #endif
  49. }
  50.  
  51. //
  52. //
  53. //
  54. TOpenSaveDialog::TData::TData(const TData& src)
  55. :
  56.   Flags(src.Flags), Error(0), FileName(0), Filter(0),
  57.   CustomFilter(src.CustomFilter), FilterIndex(src.FilterIndex),
  58.   InitialDir(src.InitialDir), DefExt(src.DefExt),
  59.   MaxPath(src.MaxPath)
  60. {
  61.   FileName = strnewdup(src.FileName, MaxPath);
  62.   SetFilter(src.Filter);
  63. }
  64.  
  65. //
  66. //
  67. //
  68. TOpenSaveDialog::TData::~TData()
  69. {
  70.   delete[] FileName;
  71.   delete[] Filter;
  72. }
  73.  
  74. //
  75. //
  76. //
  77. TOpenSaveDialog::TData&
  78. TOpenSaveDialog::TData::operator =(const TData& src)
  79. {
  80.   Flags = src.Flags;
  81.   Error = 0;
  82.   CustomFilter = src.CustomFilter;
  83.   FilterIndex = src.FilterIndex;
  84.   InitialDir = src.InitialDir;
  85.   DefExt = src.DefExt;
  86.   MaxPath = src.MaxPath;
  87.  
  88.   delete[] FileName;
  89.   FileName = strnewdup(src.FileName, MaxPath);
  90.  
  91.   SetFilter(src.Filter);
  92.  
  93.   return *this;
  94. }
  95.  
  96. //
  97. // Set the file list box filter strings. Translates '|'s into 0s so that the
  98. // string can be kept as a resource with imbeded '|'s like:
  99. // "Text Files(*.txt)|*.TXT|All Files(*.*)|*.*|"
  100. // Can also handle already processed filter strings.
  101. //
  102. void
  103. TOpenSaveDialog::TData::SetFilter(const char far* filter)
  104. {
  105.   // Copy filter string
  106.   //
  107.   if (filter) {
  108.     delete[] Filter;
  109.     if (strchr(filter, '|')) {
  110.       uint len = strlen(filter) + 2; // one for each terminating 0
  111.       Filter = strcpy(new char[len], filter);
  112.       Filter[len-1] = 0;             // in case trailing '|' is missing
  113.     }
  114.     else {
  115.       const char far* p = filter;
  116.       while (*p)
  117.         p += strlen(p) + 1;             // scan for 00 at end
  118.       uint len = uint(p - filter) + 1;  // one more for last 0
  119.       Filter = new char[len];
  120.       memcpy(Filter, filter, len);
  121.     }
  122.   }
  123.   // Stomp |s with 0s
  124.   //
  125.   if (Filter) {
  126.     for (TCharIterator<char> i(Filter); ; i++) {
  127.       if (!*i.Current())
  128.         break;
  129.       if (*i.Current() == '|')
  130.         *i.Current() = 0;
  131.     }
  132.   }
  133. }
  134.  
  135.  
  136. //----------------------------------------------------------------------------
  137.  
  138. DEFINE_RESPONSE_TABLE1(TOpenSaveDialog, TCommonDialog)
  139. END_RESPONSE_TABLE;
  140.  
  141. uint TOpenSaveDialog::ShareViMsgId = 0;
  142.  
  143.  
  144. //
  145. //
  146. //
  147. void
  148. TOpenSaveDialog::Init(TResId templateId)
  149. {
  150.   if (!ShareViMsgId)
  151.     ShareViMsgId = ::RegisterWindowMessage(SHAREVISTRING);
  152.  
  153.   memset(&ofn, 0, sizeof(OPENFILENAME));
  154.   ofn.lStructSize = sizeof(OPENFILENAME);
  155.   ofn.hwndOwner = Parent ? Parent->GetHandle() : 0;
  156.   ofn.hInstance = *GetModule();
  157.   ofn.Flags = OFN_ENABLEHOOK | Data.Flags;
  158.   if (templateId) {
  159.     ofn.lpTemplateName = templateId;
  160.     ofn.Flags |= OFN_ENABLETEMPLATE;
  161.   }
  162.   else
  163.     ofn.Flags &= ~OFN_ENABLETEMPLATE;
  164.   ofn.lpfnHook = 0;
  165.  
  166.   ofn.lpstrFilter = Data.Filter;
  167.   ofn.nFilterIndex = Data.FilterIndex;
  168.   ofn.lpstrFile = Data.FileName;
  169.   ofn.nMaxFile = Data.MaxPath;
  170.   ofn.lpstrInitialDir = Data.InitialDir;
  171.   ofn.lpstrDefExt = Data.DefExt;
  172. }
  173.  
  174. //
  175. //
  176. //
  177. TOpenSaveDialog::TOpenSaveDialog(TWindow* parent, TData& data, TModule*   module)
  178. :
  179.   TCommonDialog(parent, 0, module),
  180.   Data(data)
  181. {
  182. }
  183.  
  184. //
  185. //
  186. //
  187. TOpenSaveDialog::TOpenSaveDialog(TWindow*        parent,
  188.                                  TData&          data,
  189.                                  TResId          templateId,
  190.                                  const char far* title,
  191.                                  TModule*        module)
  192. :
  193.   TCommonDialog(parent, 0, module),
  194.   Data(data)
  195. {
  196.   Init(templateId);
  197.   ofn.lpstrTitle = title;
  198. }
  199.  
  200. //
  201. //
  202. //
  203. bool
  204. TOpenSaveDialog::DialogFunction(uint msg, TParam1 param1, TParam2 param2)
  205. {
  206.   if (TCommonDialog::DialogFunction(msg, param1, param2))
  207.     return true;
  208.  
  209.   if (msg == TOpenSaveDialog::ShareViMsgId)
  210.     return (bool)ShareViolation();
  211.  
  212.   return false;
  213. }
  214.  
  215. //
  216. //
  217. //
  218. int
  219. TOpenSaveDialog::ShareViolation()
  220. {
  221.   return OFN_SHAREWARN;
  222. }
  223.  
  224. //----------------------------------------------------------------------------
  225.  
  226. //
  227. //
  228. //
  229. TFileOpenDialog::TFileOpenDialog(TWindow*        parent,
  230.                                  TData&          data,
  231.                                  TResId          templateId,
  232.                                  const char far* title,
  233.                                  TModule*        module)
  234. :
  235.   TOpenSaveDialog(parent, data, templateId, title, module)
  236. {
  237. }
  238.  
  239. //
  240. //
  241. //
  242. int
  243. TFileOpenDialog::DoExecute()
  244. {
  245.   ofn.lpfnHook = LPOFNHOOKPROC(StdDlgProc);
  246.   int ret = ::GetOpenFileName(&ofn);
  247.   if (ret) {
  248.     Data.Flags = ofn.Flags;
  249.     Data.FilterIndex = (int)ofn.nFilterIndex;
  250.     Data.Error = 0;
  251.   }
  252.   else {
  253.     Data.Error = ::CommDlgExtendedError();
  254.   }
  255.   return ret ? IDOK : IDCANCEL;
  256. }
  257.  
  258.  
  259. //----------------------------------------------------------------------------
  260.  
  261. //
  262. //
  263. //
  264. TFileSaveDialog::TFileSaveDialog(TWindow*        parent,
  265.                                  TData&          data,
  266.                                  TResId          templateId,
  267.                                  const char far* title,
  268.                                  TModule*        module)
  269. :
  270.   TOpenSaveDialog(parent, data, templateId, title, module)
  271. {
  272. }
  273.  
  274. //
  275. //
  276. //
  277. int
  278. TFileSaveDialog::DoExecute()
  279. {
  280.   ofn.lpfnHook = LPOFNHOOKPROC(StdDlgProc);
  281.   int ret = ::GetSaveFileName(&ofn);
  282.   if (ret) {
  283.     Data.Flags = ofn.Flags;
  284.     Data.FilterIndex = ofn.nFilterIndex;
  285.     Data.Error = 0;
  286.   }
  287.   else {
  288.     Data.Error = ::CommDlgExtendedError();
  289.   }
  290.   return ret ? IDOK : IDCANCEL;
  291. }
  292.  
  293. #endif
  294. #if !defined(SECTION) || SECTION == 2
  295.  
  296. //
  297. //
  298. //
  299. void
  300. TOpenSaveDialog::TData::Read(ipstream& is)
  301. {
  302.   is >> Flags;
  303.   FileName = is.readString();
  304.   Filter = is.freadString();
  305.   CustomFilter = is.freadString();
  306.   is >> FilterIndex;
  307.   InitialDir = is.freadString();
  308.   DefExt = is.freadString();
  309. }
  310.  
  311. //
  312. //
  313. //
  314. void
  315. TOpenSaveDialog::TData::Write(opstream& os)
  316. {
  317.   os << Flags;
  318.   os.writeString(FileName);
  319.   os.fwriteString(Filter);
  320.   os.fwriteString(CustomFilter);
  321.   os << FilterIndex;
  322.   os.fwriteString(InitialDir);
  323.   os.fwriteString(DefExt);
  324. }
  325.  
  326. #endif
  327.